from django.contrib import admin
from .models import Follow, Comment, Favorite, ReadingGroup, GroupMembership, Post


@admin.register(Follow)
class FollowAdmin(admin.ModelAdmin):
    """关注关系管理"""
    
    list_display = ('id', 'follower', 'following', 'created_at')
    search_fields = ('follower__username', 'following__username')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('关注信息', {
            'fields': ('follower', 'following')
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    """评论管理"""
    
    list_display = ('id', 'book', 'user', 'content_preview', 'like_count', 'created_at')
    list_filter = ('created_at',)
    search_fields = ('book__title', 'user__username', 'content')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('评论信息', {
            'fields': ('book', 'user', 'content', 'parent', 'like_count')
        }),
    )
    
    readonly_fields = ('created_at', 'like_count')
    list_per_page = 20
    date_hierarchy = 'created_at'
    
    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '评论内容'


@admin.register(Favorite)
class FavoriteAdmin(admin.ModelAdmin):
    """收藏管理"""
    
    list_display = ('id', 'user', 'book', 'created_at')
    search_fields = ('user__username', 'book__title')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('收藏信息', {
            'fields': ('user', 'book')
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20


class GroupMembershipInline(admin.TabularInline):
    """小组成员内联"""
    model = GroupMembership
    extra = 1
    fields = ('user', 'role', 'joined_at')
    readonly_fields = ('joined_at',)


@admin.register(ReadingGroup)
class ReadingGroupAdmin(admin.ModelAdmin):
    """读书小组管理"""
    
    list_display = ('id', 'name', 'creator', 'is_public', 'max_members', 'created_at')
    list_filter = ('is_public', 'created_at')
    search_fields = ('name', 'creator__username', 'description')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'description', 'creator', 'cover_image')
        }),
        ('设置信息', {
            'fields': ('is_public', 'max_members')
        }),
    )
    
    readonly_fields = ('created_at',)
    inlines = [GroupMembershipInline]
    list_per_page = 20


@admin.register(GroupMembership)
class GroupMembershipAdmin(admin.ModelAdmin):
    """小组成员关系管理"""
    
    list_display = ('id', 'group', 'user', 'role', 'joined_at')
    list_filter = ('role', 'joined_at')
    search_fields = ('group__name', 'user__username')
    ordering = ('-joined_at',)
    
    fieldsets = (
        ('成员信息', {
            'fields': ('group', 'user', 'role')
        }),
    )
    
    readonly_fields = ('joined_at',)
    list_per_page = 20


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    """动态管理"""
    
    list_display = (
        'id', 'user', 'content_preview', 'book', 'group', 
        'like_count', 'comment_count', 'created_at'
    )
    list_filter = ('created_at',)
    search_fields = ('user__username', 'content', 'book__title', 'group__name')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('动态信息', {
            'fields': ('user', 'content', 'images')
        }),
        ('关联信息', {
            'fields': ('book', 'group')
        }),
        ('统计信息', {
            'fields': ('like_count', 'comment_count')
        }),
    )
    
    readonly_fields = ('created_at', 'like_count', 'comment_count')
    list_per_page = 20
    date_hierarchy = 'created_at'
    
    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '动态内容'
